package io;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.io.*;
import java.util.List;
import java.util.Map;

public class IoDemo {

  public static void main(String[] args) {

    List<Alias> railway = getAlias();
    Map<String, Integer> compareAlias = getCompareAliasMap();
    printOut(railway,compareAlias);

  }

  private static void printOut(List<Alias> railway, Map<String, Integer> compareAlias) {
    for(Alias alias:railway){
      for(String aliasString : alias.aliasArr){
        Integer integer = compareAlias.get(aliasString);
        if(integer!=null){
          alias.copy++;
        }
      }
    }

    try {
      PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream("\\Users\\yincy\\Desktop\\?????\\?????1887????\\res")));
      for (Alias alias:railway){
        pw.println(alias.toString());
      }
      pw.flush();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }finally {

    }
  }

  private static Map<String, Integer> getCompareAliasMap() {
    Map<String, Integer> railway = Maps.newHashMapWithExpectedSize(610000);
    File file = new File("\\Users\\yincy\\Desktop\\?????\\?????1887????\\61alias.txt");
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis);
      BufferedReader br = new BufferedReader(isr);
      int cnt = 0;
      Long start = System.currentTimeMillis();
      //??????
      while (br.ready()) {
        for (String alias : spilt(br.readLine().trim())) {
          cnt++;
          if (cnt % 10000 == 0) {
            System.out.println(cnt + "cost time" + (System.currentTimeMillis() - start));
            start = System.currentTimeMillis();
          }
          System.out.println(alias);
          railway.put(alias, new Integer(1));
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return railway;
  }

  public static List<Alias> getAlias() {
    List<Alias> railway = Lists.newArrayList();
    File file = new File("D:\\Users\\yincy\\Desktop\\?????\\?????1887????\\1887alias.txt");
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis);
      BufferedReader br = new BufferedReader(isr);
      //??????
      while (br.ready()) {
        String alias = br.readLine().trim();
        Alias alias1 = new Alias(alias);
        System.out.println(alias1);
        railway.add(alias1);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return railway;
  }

  /**
   * alias??
   *
   * @param row
   * @return
   */
  public static String[] spilt(String row) {
    String[] string = row.substring(1, row.length() - 1).split("\\|");
    return string;
  }

  private static class Alias {
    String alias;
    String[] aliasArr;
    int copy = 0;

    public Alias(String alias) {
      this.alias = alias;
      aliasArr = IoDemo.spilt(alias);
    }

    @Override
    public String toString() {
      return  alias + "\t" +copy;
    }
  }

}
